<script setup lang="ts">
import {onMounted, onUnmounted} from "vue";
import {messageStore} from "./store";


import {appWindow, WebviewWindow} from '@tauri-apps/api/window'
const webview = new WebviewWindow('theUniqueLabel', {
  url: '#/login',
  height: 500,
  width: 350,
  resizable: false,
  focus: true,
  title: 'Mini Chat Login'
})
// since the webview window is created asynchronously,
// Tauri emits the `tauri://created` and `tauri://error` to notify you of the creation response
webview.once('tauri://created', function () {
  appWindow.hide()
  console.log('create window ok')
})
webview.once('tauri://error', function (e) {
  console.error(e)
})


const store = messageStore()
let timeId = store.timeId
window.localStorage.setItem('ernie-chat-token', 'see-you-tonight')
const html = document.querySelector("html") as HTMLElement
const theme: string | null = window.localStorage.getItem("mini_chat_theme")
if (theme === null || theme === undefined) {
  html?.setAttribute('data-theme', 'dark')
} else html?.setAttribute('data-theme', theme)

onMounted(() => timeId = setInterval(() => {
  store.setDate()
}, 1000))

onUnmounted(() => {
  clearInterval(timeId)
})


</script>
<template>
  <RouterView></RouterView>
</template>

<style scoped>
</style>
